package cn.net.cfss.fgbp.face.console.controller;

import cn.net.cfss.fgbp.api.console.dto.SysMenuDto;
import cn.net.cfss.fgbp.api.console.inte.ISysMenuService;
import cn.net.cfss.fgbp.api.console.vo.SysMenuVo;
import cn.net.cfss.fgbp.base.common.GlobalConstants;
import cn.net.cfss.fgbp.base.constant.ErrCodeEnum;
import cn.net.cfss.fgbp.base.controller.BaseController;
import cn.net.cfss.fgbp.face.console.util.ReturnUtil;
import cn.net.cfss.fgbp.face.console.vo.BaseParas;
import cn.net.cfss.fgbp.log.influxdb.ApiDto;
import cn.net.cfss.fgbp.log.statistics.IStatsdLogService;
import cn.net.cfss.fgbp.log.statistics.LogDtoFactory;
import cn.net.cfss.fgbp.log.util.LogStandardUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @author oygq
 * @title: SysMenuController
 * @description: TODO
 * @date 2019/4/3 14:55
 */

@RestController
@Slf4j
@RequestMapping("console/menu")
public class SysMenuController extends BaseController {

    @Autowired
    ISysMenuService menuService;

    @ApiOperation("菜单新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String inset(@RequestBody BaseParas paras){
        SysMenuVo vo = JSONObject.toJavaObject(paras.getPlain(),SysMenuVo.class);
        if(!menuService.uniquenessCheck(vo.getPermission(), vo.getId())){
            return ReturnUtil.returnErr("权限标识已存在");
        }
        vo.setCreateBy(Long.parseLong(request.getHeader(GlobalConstants.HEADER_USERID)));
        if(menuService.insert(vo)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("菜单修改")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String update(@RequestBody BaseParas paras){
        SysMenuVo vo = JSONObject.toJavaObject(paras.getPlain(),SysMenuVo.class);
        if(!menuService.uniquenessCheck(vo.getPermission(), vo.getMenuId())){
            return ReturnUtil.returnErr("权限标识已存在");
        }
        vo.setUpdateBy(Long.parseLong(request.getHeader(GlobalConstants.HEADER_USERID)));
        //清空空数据
        if (vo.getIcon() == null) {
            vo.setIcon("");
        }
        if (vo.getComponent() == null) {
            vo.setComponent("");
        }
        if (vo.getUrl() == null) {
            vo.setUrl("");
        }
        if (vo.getInterfaceUrl() == null) {
            vo.setInterfaceUrl("");
        }
        if (vo.getRemark() == null) {
            vo.setRemark("");
        }
        if(menuService.update(vo)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("菜单删除")
    @RequestMapping(value = "/delete/{menuId}", method = RequestMethod.POST)
    public String update(@PathVariable Long menuId){

        if(null == menuId){
            return ReturnUtil.returnErr("菜单为空");
        }
        if(menuService.delete(menuId)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("菜单删除")
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public String deleteBatch(@RequestBody BaseParas paras){
        List<Long> orgIds = (List<Long>) paras.getPlain().get("menuIds");

        if(null == orgIds || orgIds.size() == 0){
            return ReturnUtil.returnErr(ErrCodeEnum.ERR_VAII);
        }
        if(menuService.deleteBatch(orgIds)){
            return ReturnUtil.returnSucc();
        }
        return ReturnUtil.returnErr(ErrCodeEnum.ERR);
    }

    @ApiOperation("根据id查询菜单")
    @RequestMapping(value = "/query/{menuId}", method = RequestMethod.POST)
    public String queryById(@PathVariable Long menuId){
        if(null == menuId){
            return ReturnUtil.returnErr("菜单为空");
        }
        SysMenuDto dto = menuService.query(menuId);
        return ReturnUtil.returnSucc(dto);
    }

    @ApiOperation("查询菜单树")
    @RequestMapping(value = "/query/tree", method = RequestMethod.POST)
    public String queryTree(@RequestBody BaseParas paras){
        SysMenuVo vo = JSONObject.toJavaObject(paras.getPlain(),SysMenuVo.class);
        vo.setUserId(getUserId());
        List<SysMenuDto> tree = menuService.tree(vo);
        return ReturnUtil.returnSucc(tree);
    }

    @ApiOperation("查询菜单按钮树")
    @RequestMapping(value = "/query/list", method = RequestMethod.POST)
    public String queryMenuButtonTree(@RequestBody BaseParas paras){
        SysMenuVo vo = JSONObject.toJavaObject(paras.getPlain().getJSONObject("search"),SysMenuVo.class);
        vo.setUserId(getUserId());
        List<SysMenuDto> tree = menuService.menuButtonTree(vo);
        return ReturnUtil.returnSucc(tree);
    }

    @ApiOperation("菜单的唯一性校验")
    @RequestMapping(value = "/uniquenessCheck", method = RequestMethod.POST)
    public String uniquenessCheck(@RequestBody BaseParas paras) {
        try {
            String permission = paras.getPlain().getString("code");
            Long id = paras.getPlain().getLong("id");
            return ReturnUtil.returnSucc(menuService.uniquenessCheck(permission,id));
        }catch (Exception e){
            log.error("菜单的唯一性校验请求失败！",e.getMessage());
            return ReturnUtil.returnErr("菜单的唯一性校验请求失败！");
        }

    }


}
